#include "ScreenManagerDebugImpl.h"

#include <game/ServiceProvider.h>
#include <game/debug/DebugService.h>
#include <game/ScreenManager.h>
#include <game/flow/Manager.h>
#include <game/flow/EntryDefinition.h>
#include <game/flow/EntryExitDefinition.h>

#include <system/assert.h>

ARELLO_GAME_NAMESPACE_DECL_START

ScreenManagerDebugImpl::ScreenManagerDebugImpl(ScreenManager& sm)
	: screenManager(sm)
{ }

ScreenManagerDebugImpl::~ScreenManagerDebugImpl()
{ }

void ScreenManagerDebugImpl::InvokeScreenChanged(const std::string& newScreen)
{
	this->currentScreenName = newScreen;
	this->screenChanged(newScreen);
}

const std::string& ScreenManagerDebugImpl::GetCurrentScreenName()
{
	return this->currentScreenName;
}

std::vector<std::string> ScreenManagerDebugImpl::GetExitNames()
{
	std::vector<std::string> result;

	flow::Manager* flowManager = this->screenManager.flowManager;
	if (!flowManager)
	{
		LIBA_FAIL("Flow manager is not defined");
		return result;
	}

	flow::EntryDefinition* currentEntry = screenManager.currentEntry;
	if (!currentEntry)
	{
		LIBA_FAIL("Flow manager reported that no current entry exists");
		return result;
	}

	for (std::vector<flow::EntryExitDefinition*>::const_iterator i = currentEntry->ExitsBegin();
		i != currentEntry->ExitsEnd(); ++i)
	{
		result.push_back((*i)->GetName());
	}

	return result;
}

void ScreenManagerDebugImpl::ScheduleSwitch(const std::string& exitId)
{
	screenManager.ScheduleSwitch(exitId);
}

void ScreenManagerDebugImpl::ScheduleRestart()
{
	screenManager.ScheduleRestart();
}

ScreenManagerDebugImpl* Register(std::auto_ptr<ScreenManagerDebugImpl> debugImpl, ServiceProvider& sp)
{
	debug::DebugService* debugService =
		sp.Query<debug::DebugService>();

	if (!debugService)
	{
		LIBA_FAIL("Debug service isn't registered in the service provider");
		return 0;
	}

	ScreenManagerDebugImpl* result = debugImpl.get();
	debugService->Register(debugImpl);
	return result;
}

ARELLO_GAME_NAMESPACE_DECL_END